<template>
  <div class="container">

    <div class="input-group" v-if="SearchState">
      <input type="text" class="input-field" v-model="SearchValue" placeholder="请输入内容..." />
      <button class="submit-btn" @click="submit">搜索</button>
    </div>

    <div class="grid-container">
      <div class="card" v-for="item in 20" :key="item" @click="onDetailLink(item)">
        <img src="/logo.png" alt="Card Image" class="card-img" />
        <div class="card-body">
          <div class="card-tags">
            <span class="tag" v-for="item in 2" :key="item" @click.stop="onGroupingLink(item)">{{ `标签${item}` }}</span>
            <!-- 添加更多标签 -->
          </div>
          <h3 class="card-title">卡片标题</h3>
          <p class="card-description">这里是一段描述文本，可以是关于卡片内容的简短介绍。</p>
        </div>
      </div>
    </div>

    <div class="pagination">
      <button @click="previousPage" v-if="currentPage != 1">上一页</button>
      <!-- <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span> -->
      <input type="text" v-model="currentPage" min="1" :max="totalPages" @keyup.enter="jumpToPage" />
      <span>/ {{ totalPages }} </span>
      <button @click="nextPage" v-if="currentPage != totalPages">下一页</button>
    </div>
  </div>
</template>

<script setup lang="ts">
const currentPage = ref<any>(1); // 当前页码
const totalPages = ref(10); // 总页数，这个值应根据实际情况动态获取或计算

const previousPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--;
  }
};

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++;
  }
};

const jumpToPage = () => {
  // 获取用户输入的页码
  const pageNumber = parseInt(currentPage.value);

  // 验证输入是否为数字且在有效范围内
  if (!isNaN(pageNumber) && pageNumber >= 1 && pageNumber <= totalPages.value) {
    // 跳转到用户输入的页码
    currentPage.value = pageNumber;
  } else {
    // 如果输入无效，可以给出提示或者重置为当前页
    alert("请输入有效的页码！");
    // 这里可以选择不进行任何操作保持当前页，或者重置currentPage的值
  }
};

const router = useRouter();

const onGroupingLink = (item: any) => {
  router.push({ path: `/grouping/${item}` });
};

const onDetailLink = (item: any) => {
  router.push({ path: `/appraise/${item}` });
};

const SearchState: any = useSearch();

const SearchValue = ref("");

const submit = () => {
  console.log(SearchValue.value);
  router.push({ path: `/search/${SearchValue.value}` });
};
</script>

<style scoped>
.container {
  width: 1600px;
  margin: 20px auto;
  max-width: 100%;
  box-sizing: border-box;
}

.card {
  border-radius: 10px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  overflow: hidden; /* 确保图片尺寸不会超出卡片边界 */
}

.card-img {
  width: 100%;
  height: auto;
  display: block;
}

.card-body {
  padding: 16px;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
}

.tag {
  background-color: #f1f1f1;
  color: #333;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.9em;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* 这里定义了4列，每列宽度相等 */
  grid-gap: 20px; /* 设置网格之间的间隔，可根据需要调整 */
}

.card-title {
  margin: 0 0 8px 0;
  font-size: 1.2em;
  color: #333;
}

.card-description {
  color: #666;
  line-height: 1.5;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}

.pagination button {
  background-color: #4caf50;
  border: none;
  color: white;
  padding: 8px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 5px;
}

.pagination button[disabled] {
  opacity: 0.6;
  cursor: not-allowed;
}

.pagination span {
  font-weight: bold;
  color: #333;
}

.pagination input[type="text"] {
  /* 设置基本样式 */
  width: 40px; /* 调整宽度以适应你的布局 */
  padding: 5px 10px; /* 内边距，使得文本居中且有舒适的点击区域 */
  margin: 0 5px; /* 与其他元素保持适当距离 */
  text-align: center; /* 文本居中 */
  border: 1px solid #ccc; /* 边框样式 */
  border-radius: 4px; /* 边框圆角 */
  font-size: 14px; /* 字体大小 */

  /* 更美观的悬停和聚焦状态 */
  transition: border-color 0.3s ease;
}

.pagination input[type="text"]:hover,
.pagination input[type="text"]:focus {
  border-color: #4caf50; /* 鼠标悬停或聚焦时边框颜色改变 */
  outline: none; /* 移除默认的轮廓，若不需要 */
}

/* 可选：统一输入框与按钮的样式，例如颜色和字体 */
.pagination input[type="text"],
.pagination button {
  color: #333;
  font-family: Arial, sans-serif; /* 或其他字体 */
}

/* 可选：增加一些反馈效果，比如当输入无效时的样式 */
.pagination input[type="text"].invalid {
  border-color: #f44336; /* 红色边框表示错误 */
}

.card:hover {
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
  transform: scale(1.05);
}

.tag:hover {
  background-color: #007bff; /* 或者你喜欢的高亮颜色 */
  color: #fff; /* 文字颜色，确保在高亮背景下可读 */
}


.input-group {
  position: relative;
  width: 700px;
  margin: 20px auto;
}

.input-field {
  width: 600px;
  padding: 15px 20px;
  border-radius: 25px;
  border: 1px solid #ddd;
  outline: none;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.05);
  font-size: 14px;
  transition: all 0.3s ease;
}

.input-field:focus {
  border-color: #007bff;
  box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
}

/* 按钮样式 */
.submit-btn {
  position: absolute;
  right: 0;
  top: 0;
  width: 100px;
  height: 100%;
  padding: 0 20px;
  border: none;
  outline: none;
  background-color: #007bff;
  color: white;
  cursor: pointer;
  border-radius: 0 25px 25px 0;
  font-size: 14px;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background-color: #0056b3;
}

/* 输入框聚焦时按钮效果 */
.input-field:focus + .submit-btn {
  background-color: #0056b3;
}
</style>
